package pl.gladiator.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import pl.gladiator.model.Newsy;
import pl.gladiator.model.User;

public class UserEntityManagerImp implements UserEntityManager {
	public static Logger log = Logger.getLogger(UserEntityManagerImp.class);
	private Connection connect;
	private ResultSet result = null;
	private PreparedStatement preparedStatement = null;

	public void connectWithDB() {
		try {
			String url = "jdbc:mysql://misiekmk.linuxpl.info/misiekmk_uks?useOldAliasMetadataBehavior=true&characterEncoding=utf8&"
					+ "user=misiekmk_uks&password=uks";
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection(url);
			if (log.isInfoEnabled())
				log.info("Polaczono z BD");
		} catch (ClassNotFoundException e) {
			log.fatal(e.getMessage());
		} catch (SQLException e) {
			log.fatal(e.getMessage());
		}

	}

	@Override
	public void save(User user) {
		connectWithDB();
		try {
			preparedStatement = connect
					.prepareStatement("insert into user values(default, ?, ?, ?, ?, ?, ?, ?, now())");
			preparedStatement.setString(1, user.getLogin());
			preparedStatement.setString(2, user.getPassword());
			preparedStatement.setString(3, user.getFirstname());
			preparedStatement.setString(4, user.getLastname());
			preparedStatement.setString(5, user.getMail());
			preparedStatement.setString(6, "u");
			preparedStatement.setString(7, "NIE");
			preparedStatement.executeUpdate();

			if (this.preparedStatement != null) {
				this.preparedStatement.close();
			}

		} catch (SQLException e) {
			log.fatal(e.getMessage());
		} finally {
			if (connect != null) {
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	@Override
	public List<User> getAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(int pk, String update) {
		if (log.isDebugEnabled())
			log.debug(pk);
		try {
			connectWithDB();
			String query = "update user set activate = ? where userPk = ?";
			PreparedStatement st = connect.prepareStatement(query);

			st.setString(1, update);
			st.setInt(2, pk);

			st.executeUpdate();
			st.close();

			if (this.preparedStatement != null) {
				this.preparedStatement.close();
			}

		} catch (SQLException e) {
			log.fatal(e.getMessage());
		} finally {
			if (connect != null) {
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	@Override
	public void delete(int pk) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<User> getNotActivatedUsers() {
		List<User> userList = new ArrayList<User>();
		connectWithDB();
		try {
			this.preparedStatement = this.connect
					.prepareStatement("select userPk, login, firstname, lastname, mail  from user where activate = 'NIE'");
			result = this.preparedStatement.executeQuery();
			while (result.next()) {
				User user = new User(result.getInt("userPk"),
						result.getString("login"),
						result.getString("firstname"),
						result.getString("lastname"), result.getString("mail"));
				userList.add(user);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			if (connect != null) {
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
		return userList;
	}

	@Override
	public List<User> getActivatedUsers() {
		List<User> userList = new ArrayList<User>();
		connectWithDB();
		try {
			this.preparedStatement = this.connect
					.prepareStatement("select userPk, login, firstname, lastname, mail  from user where activate = 'TAK'");
			result = this.preparedStatement.executeQuery();
			while (result.next()) {
				User user = new User(result.getInt("userPk"),
						result.getString("login"),
						result.getString("firstname"),
						result.getString("lastname"), result.getString("mail"));
				userList.add(user);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			if (connect != null) {
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
		return userList;
	}

	@Override
	public User findUserByPk(int pk) {
		User user = null;
		connectWithDB();
		try {
			this.preparedStatement = this.connect
					.prepareStatement("select firstname, lastname, mail, registerDate  from user where activate = 'TAK'");
			result = this.preparedStatement.executeQuery();
			while (result.next()) {
				user = new User(result.getString("firstname"),
						result.getString("lastname"), result.getString("mail"),
						result.getDate("registerDate"));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			if (connect != null) {
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
		return user;
	}

	@Override
	public User checkLogin(User user) {
		connectWithDB();
		log.info(user);
		User userDB = null;
		try {
			this.preparedStatement = this.connect
					.prepareStatement("select userPk, firstname, lastname, login, type, activate from user where login = '"+ user.getLogin() + "' and password = '" + user.getPassword() + "' ");
			result = this.preparedStatement.executeQuery();
			while (result.next()) {
				userDB = new User(result.getInt("userPk"), result.getString("login"), result.getString("firstname"), result.getString("lastname"), result.getString("type"), result.getString("activate"));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			if (connect != null) {
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
		return userDB;
	}

}
